package com.codi.bus.core.sys.web.portfolio.controller;

import com.codi.base.domain.BaseResult;
import com.codi.bus.core.dao.FundWarehouseMappingDao;
import com.codi.bus.core.domain.*;
import com.codi.bus.core.service.*;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Slf4j
@RequestMapping("/portfolio/query")
public class PortfolioQueryController {
    @Autowired
    private FundCombinationService fundCombinationService;

    @Autowired
    private FundCombinationMappingService fundCombinationMappingService;

    @Autowired
    private FundCombinationHistoryService fundCombinationHistoryService;

    @Autowired
    private FundWarehouseTransferService fundWarehouseTransferService;

    @Autowired
    private FundWarehouseMappingService fundWarehouseMappingService;

    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    @GetMapping("queryc_id")
    public BaseResult qyerC(Long combinationId) {
        return BaseResult.success(fundCombinationService.queryByPrimaryKey(combinationId));
    }


    @GetMapping
    public BaseResult query(Pageable pageable) {
        return fundCombinationService.queryPorfolio(pageable);
    }

    @PostMapping
    public BaseResult add(FundCombination fundCombination) {
        fundCombinationService.addPorfolio(fundCombination);

        return BaseResult.success();
    }

    @PostMapping("/update")
    public BaseResult update(FundCombination fundCombination) {
        fundCombinationService.updatePorfolio(fundCombination);

        return BaseResult.success();
    }

    @PostMapping("/getOne")
    public BaseResult getOne(String combinationCode) {
        if (!Strings.isNullOrEmpty(combinationCode)) {
            return BaseResult.success(fundCombinationService.queryOne(combinationCode));
        }

        return BaseResult.success();
    }

    @PostMapping("/get_mapping")
    public BaseResult getMapping(String combinationCode) {
        if (!Strings.isNullOrEmpty(combinationCode)) {
            return BaseResult.success(fundCombinationMappingService.queryByCombinationCode(combinationCode));
        }

        return BaseResult.success();
    }

    @PostMapping("/add_mapping")
    public BaseResult addMapping(FundCombinationMapping mapping) {
        return BaseResult.success(fundCombinationMappingService.add(mapping));
    }

    @PostMapping("/delete_mapping")
    public BaseResult deleteMapping(Long mappingId) {
        return BaseResult.success(fundCombinationMappingService.delete(mappingId));
    }

    @GetMapping("/query_mapping")
    public BaseResult queryMapping(Long mappingId) {
        return BaseResult.success(fundCombinationMappingService.queryByPrimaryKey(mappingId));
    }

    @PostMapping("/update_mapping")
    public BaseResult updateMapping(FundCombinationMapping mapping) {
        return BaseResult.success(fundCombinationMappingService.update(mapping));
    }

    @GetMapping("/delete")
    public BaseResult deleteCombination(String combinationCode) {
        if (!Strings.isNullOrEmpty(combinationCode)) {
            fundCombinationService.deleteByCombinationCode(combinationCode);
            fundCombinationMappingService.deleteCombination(combinationCode);
            return BaseResult.success();
        }

        return BaseResult.success();
    }

    @GetMapping("/history")
    public BaseResult getHistory(Pageable pageable) {
        return BaseResult.success(fundCombinationHistoryService.queryByPage(pageable));
    }

    @GetMapping("/history_code")
    public BaseResult getHistoryByCode(Pageable pageable, String combinationCode) {
        return BaseResult.success(fundCombinationHistoryService.queryByPageAndCombinationCode(pageable, combinationCode));
    }

    @GetMapping("/delete_history")
    public BaseResult deleteHis(Long combinationId) {
        return BaseResult.success(fundCombinationHistoryService.delete(combinationId));
    }

    @GetMapping("query_id")
    public BaseResult queryId(Long combinationId) {
        return BaseResult.success(fundCombinationHistoryService.queryById(combinationId));
    }

    @GetMapping("update_id")
    public BaseResult updateId(FundCombinationHistory fundCombinationHistory) {
        return BaseResult.success(fundCombinationHistoryService.updateById(fundCombinationHistory));
    }

    @PostMapping("add_his")
    public BaseResult addHis(FundCombinationHistory fundCombinationHistory) {
        return BaseResult.success(fundCombinationHistoryService.insertFundHistory(fundCombinationHistory));
    }

    @GetMapping("query_transfers")
    public BaseResult queryTransfer(Pageable pageable) {
        return BaseResult.success(fundWarehouseTransferService.queryByPage(pageable));
    }

    @GetMapping("query_one_transfer")
    public BaseResult queryOneTransfer(Long wareHouseId) {
        return BaseResult.success(fundWarehouseTransferService.selectOne(wareHouseId));
    }

    @GetMapping("delete_transfer")
    public BaseResult deleteTransfer(Long wareHouseId) {
        return BaseResult.success(fundWarehouseTransferService.deleteByKey(wareHouseId));
    }

    @GetMapping("update_transfer")
    public BaseResult updateTransfer(FundWarehouseTransfer transfer) {
        return BaseResult.success(fundWarehouseTransferService.updateOne(transfer));
    }

    @GetMapping("add_transfer")
    public BaseResult addTransfer(FundWarehouseTransfer transfer) {
        return BaseResult.success(fundWarehouseTransferService.addFundWarehouseTransfer(transfer));
    }

    @GetMapping("query_all_combinations")
    public BaseResult queryAllCombinations() {
        return BaseResult.success(fundCombinationService.queryAll());
    }

    @GetMapping("query_one_warehouse")
    public BaseResult queryOneWareHouse(Long mappingId) {
        return BaseResult.success(fundWarehouseMappingService.queryOne(mappingId));
    }

    @GetMapping("add_one_warehouse")
    public BaseResult addOneHouse(FundWarehouseMapping mapping) {
        return BaseResult.success(fundWarehouseMappingService.addMapping(mapping));
    }

    @GetMapping("query_warehouses")
    public BaseResult queryWarehouses(Long warehouseId) {
        return BaseResult.success(fundWarehouseMappingService.queryByWareHouseId(warehouseId));
    }

    @GetMapping("delete_warehouse")
    public BaseResult deleteWarehouse(Long mappingId) {
        return BaseResult.success(fundWarehouseMappingService.deleteMapping(mappingId));
    }

    @GetMapping("update_warehouse")
    public BaseResult updateWarehouse(FundWarehouseMapping mapping) {
        return BaseResult.success(fundWarehouseMappingService.updateMapping(mapping));
    }
}
